package com.crm.system.service.impl.contactManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactDetail;
import com.crm.model.entity.contactManager.ContactTag;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.contactManager.ContactQO;
import com.crm.model.vo.contactManager.ContactVO;
import com.crm.model.vo.customerManager.CustomerContactVO;
import com.crm.service.contactManager.ContactDbService;
import com.crm.service.contactManager.ContactDetailDbService;
import com.crm.service.customerManager.CustomerContactPersonDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.customerManager.CustomerPoolDbService;
import com.crm.system.service.contactManager.ContactDetailService;
import com.crm.system.service.contactManager.ContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * bj
 * 联系人管理信息
 */
@Service
public class ContactDetailServiceImpl implements ContactDetailService {

    @Autowired
    private ContactDbService contactDbService;

    @Autowired
    private ContactDetailDbService contactDetailDbService;


    /**
     * 根据联系人id,客户id,联系人姓名,企业性质查询同一联系人下的联系方式
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 查询条件
     * @param sortOrder 排序列名字
     * @param customerContactVO 排序规则(asc/desc)
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public DhPageInfo<ContactDetail> contactDetailList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, CustomerContactVO customerContactVO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        String order = "create_tm desc";
        List<ContactDetail> list = contactDetailDbService.contactDetailList(order, customerContactVO.getContactId(),customerContactVO.getContactDetailType());
        PageInfo<ContactDetail> pageInfo = new PageInfo<ContactDetail>(list);
        return new DhPageInfo<ContactDetail>(pageInfo, list);
    }

    /**
     * 联系人-新增联系方式,节日节点
     * @param contactDetail 联系人实体类
     * @return
     */
    @Override
    public Boolean addContactDetail(ContactDetail contactDetail) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String contactDetailId = CommonUtils.getGUID();
        contactDetail.setContactDetailId(contactDetailId);
        contactDetail.setCreatePeopleId(userAccount.getAccountId());
        contactDetail.setCreateTm(new Date());
        contactDetail.setUpdatePeopleId(userAccount.getAccountId());
        contactDetail.setUpdateTm(new Date());
        contactDetail.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //新增联系方式
        Boolean result = contactDetailDbService.addContactDetail(contactDetail);
        return result;
    }

    /**
     * 联系人-联系人详情-联系方式重复
     * @param contactId 联系人id
     * @param contactDetailValue 联系方式
     * @return
     */
    @Override
    public ContactDetail getContactDetailIdAndContactDetailValue(String contactId, String contactDetailValue) {
        return contactDetailDbService.getContactDetailIdAndContactDetailValue(contactId,contactDetailValue);
    }

    /**
     * 联系人-修改联系方式,节日节点
     * @param contactDetail 联系详情实体
     * @return
     */
    @Override
    public Boolean updateContactDetail(ContactDetail contactDetail) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        contactDetail.setUpdatePeopleId(userAccount.getAccountId());
        contactDetail.setUpdateTm(new Date());
        return contactDetailDbService.updateContactDetail(contactDetail);
    }

    /**
     * 联系人-联系人详情-联系方式,节日节点修改回显
     * @param contactDetailId 联系方式id,节日节点id
     * @return
     */
    @Override
    public ContactDetail getContactDetailId(String contactDetailId) {
        return contactDetailDbService.getContactDetailId(contactDetailId);
    }

    /**
     * 联系人-删除联系方式,节日节点 逻辑删除
     * @param contactDetailId 联系方式id,节日节点id
     * @return
     */
    @Override
    public Boolean deleteContactDetail(String contactDetailId) {
        ContactDetail contactDetail = new ContactDetail();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        contactDetail.setContactDetailId(contactDetailId);
        contactDetail.setUpdatePeopleId(userAccount.getAccountId());
        contactDetail.setUpdateTm(new Date());
        contactDetail.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return contactDetailDbService.updateContactDetail(contactDetail);
    }

    /**
     * 新增联系人标签库
     * @return
     */
    @Override
    public String addContactTag(String tag) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        ContactTag tags = contactDetailDbService.contactTagList(tag);
        if(tags != null) {
            return "该标签已存在";
        }

        List <ContactTag> list = contactDetailDbService.contactTaList(userAccount.getAccountId());
        if(list.size() == 10) {
            return "添加标签最多为10条";
        }


        ContactTag contactTag = new ContactTag();
        String tagId = CommonUtils.getGUID();
        contactTag.setTagId(tagId);
        contactTag.setTag(tag);
        contactTag.setCreatePeopleId(userAccount.getAccountId());
        contactTag.setCreateTm(new Date());
        contactTag.setUpdatePeopleId(userAccount.getAccountId());
        contactTag.setUpdateTm(new Date());
        //新增联系方式
        Boolean result = contactDetailDbService.addContactTag(contactTag);
        return "SUCCESS";
    }

    /**
     * 查询联系人标签
     * @return
     */
    @Override
    public List<ContactTag> contactTagList() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        return contactDetailDbService.contactTaList(userAccount.getAccountId());
    }

    /**
     * 删除联系人标签库
     * @param tagId 联系人标签id
     * @return
     */
    @Override
    public Boolean deleteContactTag(String tagId) {
        ContactTag tag = new ContactTag();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        tag.setTagId(tagId);
        tag.setUpdatePeopleId(userAccount.getAccountId());
        tag.setUpdateTm(new Date());
        tag.setDeleteStatus(1);
        return contactDetailDbService.updateContactTag(tag);
    }

}
